import { ajax } from '../../utils/index';
Page({

    /**
     * 页面的初始数据
     */
    data: {
      dateTimeVisible: false,
      dateTimeText: '',
      faultTypeText: '水电',
      faultTypePickerVisible: false,
      faultTypeList: [
        { label: '水电', value: 0 },
        { label: '电器故障', value: 1 },
        { label: '照明问题', value: 2 },
        { label: '其它', value: 3 },
      ],
      gridConfig: {
        column: 4,
        width: 160,
        height: 160,
      },

      // 姓名
      name: '',
      // 手机号码
      mobile: '',
      // 门牌号
      house: '',
      // 预约上门时间
      visit_time: new Date().getTime(),
      // 故障类型
      fault_type: 0,
      // 故障图片
      file_list: [],
      // 故障描述
      fault_desc: ''
    },

    async submit() {
      const { name, mobile, house, visit_time, fault_type, file_list, fault_desc, dateTimeText } = this.data;
      console.log(name, mobile, house, visit_time, fault_type, file_list, fault_desc);
      if (name.trim().length === 0 || mobile.trim().length === 0 || house.trim().length === 0 || dateTimeText.trim().length === 0 || file_list.length === 0 || fault_desc.trim().length === 0) {
        wx.showToast({
          title: '存在必填项未填',
          icon: 'none'
        })
        return;
      }

      const { user_id,  } = wx.getStorageSync('loginInfo');
      const user_info = wx.getStorageSync('userInfo');

      const params = {
        user_id, 
        user_info,
        name, 
        mobile, 
        house, 
        visit_time, 
        fault_type, 
        file_list, 
        fault_desc
      }

      const { data } = await ajax('/add_service_order', 'POST', params);

      console.log(data);

      if (data === "success") {
        wx.switchTab({
          url: '../index/index',
          success: () => {
            wx.showToast({
              title: '发布成功',
              icon: 'none'
            })
          }
        })
      } else {
        wx.showToast({
          title: '发布失败',
          icon: 'none'
        })
      }
    },

    getName(e) {
      this.setData({
        name: e.detail.value
      })
    },

    getMobile(e) {
      this.setData({
        mobile: e.detail.value
      })
    },

    getHouse(e) {
      this.setData({
        house: e.detail.value
      })
    },

    getFaultDesc(e) {
      this.setData({
        fault_desc: e.detail.value
      })
    },

    handleRemove(e) {
      const { index } = e.detail;
      const { file_list } = this.data;

      file_list.splice(index, 1);

      this.setData({
        file_list
      })
    },

    handleAdd(e) {
      const { file_list } = this.data;
      const { files } = e.detail;

      console.log('files', files);

      const uploadTask = (item) => {
        return new Promise((resolve, reject) => {
          wx.uploadFile({
            filePath: item.url,
            name: 'file',
            url: 'http://localhost:3000/upload_file',
            success: (res) => {
              const { data } = res;
              const path = JSON.parse(data)[0].path;
              const _path = path.split('\\');

              const url = `http://localhost:3000/${_path[0]}/${_path[1]}`;

              resolve({
                ...item,
                url
              })
            },
            fail: (err) => {
              reject(null);
            }
          })
        })
      }

      Promise.all(files.map(uploadTask)).then(res => {
        this.setData({
          file_list: [...file_list, ...res]
        })
      })

    },

    onFaultTypeChange(e) {
      const { value } = e.detail;
      const { faultTypeList } = this.data;
      this.setData({
        faultTypePickerVisible: false,
        fault_type: value[0],
        faultTypeText: faultTypeList.find(item => item.value === value[0]).label,
      });
    },

    onDateTimeChange(e) {
      const { value } = e.detail;
      console.log(value);
      this.setData({
        visit_time: value,
        dateTimeText: value,
      });
    },

    showPicker() {
      this.setData({
        dateTimeVisible: true
      })
    },

    onFaultTypeClick() {
      this.setData({
        faultTypePickerVisible: true
      })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})